package test.niuke;

import java.util.Stack;


/**
 * Created by linshuo on 2018/2/23.
 *
 * 用2个堆栈实现队列
 */

public class QueueBy2Stack {
    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();

    public void push(int node) {
        stack1.push(node);
    }

    public int pop() {
        stack2.clear();
        int size = stack1.size();
        for (int i = 0; i < size; i++) {
            stack2.push(stack1.pop());
        }
        int ret = stack2.pop();
        size = stack2.size();
        for (int i = 0; i < size; i++) {
            stack1.push(stack2.pop());
        }
        return ret;
    }

    public static void main(String[] args) {
        QueueBy2Stack q1 = new QueueBy2Stack();
        q1.push(0);
        q1.push(1);
        q1.push(2);
        q1.push(3);
        System.out.println(q1.pop());
        System.out.println(q1.pop());
        System.out.println(q1.pop());
        System.out.println(q1.pop());
    }
}